<template>
  <div style="height: 100vh; overflow: hidden; position: relative" class="bgi">
    <!-- 滑块组件  accuracy表示误差范围，img是一个数组即背景图-->
    <el-card class="cover" v-if="loginAdmin.id">
      <slide-verify :l="42"
                    :r="10"
                    :w="310"
                    :h="155"
                    :accuracy="5" 
                    :imgs="['https://cdn.pixabay.com/photo/2022/11/09/12/23/lotus-7580478_960_720.jpg',
                    'https://cdn.pixabay.com/photo/2017/08/29/12/07/adult-2693054_960_720.jpg',
                    'https://cdn.pixabay.com/photo/2022/11/16/15/52/mushrooms-7596258_960_720.jpg']"
                    slider-text="向右滑动"
                    @success="onSuccess"
                    @fail="onFail"
                    @refresh="onRefresh"
      ></slide-verify>
    </el-card>

      <div style="width: 500px; height: 400px; background-color: white; border-radius: 10px;
        margin: 250px auto; padding:50px">
        <div style="margin: 30px; text-align: center; font-size: 30px; font-weight: bold; color: dodgerblue">登 录</div>
        <el-form :model="admin" :rules="rules" ref="loginForm">
          <el-form-item prop="username">
            <el-input placeholder="请输入账号" prefix-icon="el-icon-user" size="medium" v-model="admin.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" show-password prefix-icon="el-icon-lock" size="medium" v-model="admin.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%" size="medium" type="primary" @click="login">登录</el-button>
             <a href="/login" style="text-decoration:none; color:black">管理员登陆</a>
          </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from 'js-cookie'

export default {
  name: 'userLogin',
  data() {
    return {
      loginAdmin: {},
      admin: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur'},
          { min: 15, max: 20, message: '长度在15-20个字符', trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur'},
          { min: 3, max: 10, message: '长度在3-10个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          request.post('/user/login', this.admin).then(res => {
            //console.log(res);
            if (res.code === '200') {
              this.loginAdmin = res.data
              }
            else {
              this.$notify.error(res.msg)
            }
          })
        }
      })
    },
    // 滑块验证通过之后触发的
    onSuccess() { //用户和管理员同设一个属性有默认值但不一样用来分别用户和管理员
      // console.log(this.loginAdmin)
      Cookies.set('admin', JSON.stringify(this.loginAdmin))//相当于把admin的实体信息全部赋给admin,
                                                         //不管是管理员还是用户都用admin字样存储cookie
      this.$notify.success("登录成功")
      this.$router.push('/userBorrowList')
    },
    onFail() {
      //console.log('onFail')
      this.$notify.warning("请正确滑动滑块")
    },
    onRefresh() {
      //console.log('refresh')
      this.$notify.success("已刷新")
    }
  }
}
</script>

<style>
.cover {
  width: fit-content;
  background-color: white;
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
}
.bgi{
  background-image: url(../../assets/login.jpg);
}
</style>